package com.bwhx.eruler.filter;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import com.bwhx.eruler.bean.Menus;
import com.bwhx.eruler.bean.Staff;
import com.bwhx.eruler.service.MenusService;
import com.bwhx.eruler.service.impl.MenusServiceImpl;
import com.mchange.v2.async.StrandedTaskReporting;

public class LoginFilter implements Filter {
	
	@Autowired
	MenusService menusService;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        // 获得在下面代码中要用的request,response,session对象

    	HttpServletRequest servletRequest = (HttpServletRequest) request;
        HttpServletResponse servletResponse = (HttpServletResponse) response;
        HttpSession session = servletRequest.getSession();

        // 获得用户请求的URI
        String path = servletRequest.getRequestURI();
    
        // 从session里取员工工号信息
        Staff staff = (Staff) session.getAttribute("staff");

        /*创建类Constants.java，里面写的是无需过滤的页面
        for (int i = 0; i < Constants.NoFilter_Pages.length; i++) {

            if (path.indexOf(Constants.NoFilter_Pages[i]) > -1) {
                chain.doFilter(servletRequest, servletResponse);
                return;
            }
        }*/
        
        // 接口无需过滤
        if(path.indexOf("page.") == -1 || path.indexOf("page.getCaptcha.ashx") != -1|| path.indexOf("everyone") != -1 ||path.indexOf("page.401.ashx") != -1||path.indexOf("page.staffLogin.ashx") != -1||path.indexOf("page.logout.ashx") != -1 ) {
            chain.doFilter(servletRequest, servletResponse);
            return;
        }

        // 判断如果没有取到员工信息,就跳转到登陆页面
        if (staff == null || "".equals(staff)) {
            // 跳转到登陆页面 
        	/*if(request.getLocalAddr().startsWith("192")){
        		 servletResponse.sendRedirect("/bwhx");
        	}else{
        		servletResponse.sendRedirect("/");
        	}*/
        	
        	if (servletRequest.getHeader("x-requested-with") != null && servletRequest.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
        		servletResponse.setHeader("sessionstatus", "timeout");
        	}else{
        		servletResponse.sendRedirect("/");        		
        	}     	 
           
        } else {
           
        	if(staff.getsLoginName().equals("admin")){
        		chain.doFilter(request, response);
        		return;
        	}
        	/* // 已经登陆，继续此次请求
            chain.doFilter(request, response);*/
            String[] split = path.split("/");
            if(split.length==2){
            	path = split[split.length-1];
            }
            else if(split.length==3){
            	if(path.contains("bwhx")){
            		path = split[split.length-1];
            	}else{
            		path = split[split.length-2] +"/"+split[split.length-1];
            	}
            	
            }
            else{
            	path = split[split.length-2] +"/"+split[split.length-1];
            }
            
            if(path.equals("page.index.ashx")||path.equals("page.staffMenusList.ashx")){
            	chain.doFilter(request, response);
            	 return;
            }
            
            String menuIds = staff.getMenuIds();
            if(menuIds==null||menuIds.trim().length()==0){
            	/* if(request.getLocalAddr().startsWith("192")){
    	       		 servletResponse.sendRedirect("/bwhx/page.401.ashx");
    	       		 return;
    	       	}else{
    	       		servletResponse.sendRedirect("/page.401.ashx");
    	       		return;
    	       	}*/
            	 
            		
             	if (servletRequest.getHeader("x-requested-with") != null && servletRequest.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
             		servletResponse.setHeader("sessionstatus", "meiquanxian");
             	}else{
             		servletResponse.sendRedirect(servletRequest.getContextPath()+"/page.401.ashx");
             		
             	}
            }
            String[] split2 = menuIds.split(",");
            List<String> asList = Arrays.asList(split2);
            
            /*MenusService menusService = new MenusServiceImpl();*/
            List<Menus> listByStaff = menusService.listByStaff(asList, null);
            for (Menus menus : listByStaff) {
            	if(null!=menus.getmHref()&&menus.getmHref().contains(path)){
            		 // 已经登陆,已经赋权，继续此次请求
                    chain.doFilter(request, response);
                    return;
            	}
				
			}
            //若是没有赋权，转向没有权限页面
	       /* if(request.getLocalAddr().startsWith("192")){
	       		 servletResponse.sendRedirect("/bwhx/page.401.ashx");
	       	}else{
	       		servletResponse.sendRedirect("/page.401.ashx");
	       	}*/
            String ss = servletRequest.getHeader("x-requested-with");
         	if (servletRequest.getHeader("x-requested-with") != null && servletRequest.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
         		servletResponse.setHeader("sessionstatus", "meiquanxian");
         	}else{
         		servletResponse.sendRedirect(servletRequest.getContextPath()+"/page.401.ashx");
         		
         	}
            
            
        }

    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

}
